﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CollegeWebsite.Areas.Administrator.Models;

namespace CollegeWebsite.Areas.Administrator.Controllers
{
    public class FacultyController : Controller
    {

        CollegeWebsiteDataContext _dataContext = new CollegeWebsiteDataContext();

        
        //
        // GET: /Administrator/Faculty/

        public ActionResult Index()
        {
            List<Faculty> faculties = _dataContext.Faculties.Select(f => f).ToList();
            return View(faculties);
        }

        //
        // GET: /Administrator/Faculty/Details/5

        public ActionResult Details(int id)
        {
            Faculty faculty = _dataContext.Faculties.Single(f=>f.FacultyID==id);
            return View(faculty);
        }

        //
        // GET: /Administrator/Faculty/Create

        public ActionResult Create()
        {
            Faculty faculty = new Faculty();
            return View(faculty);
        } 

        //
        // POST: /Administrator/Faculty/Create

        [HttpPost, ValidateInput(false)]
        public ActionResult Create(Faculty faculty)
        {
            var fac = _dataContext.Faculties.Select(f => f).Where(f => f.FacultyName == faculty.FacultyName).FirstOrDefault();
            ViewData["name"] = fac;
            if (fac != null)
            {
                ModelState.AddModelError(string.Empty, "Name Invailid!");
            }
            if (ModelState.IsValid)
            {
                _dataContext.Faculties.InsertOnSubmit(faculty);
                _dataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
                  // TODO: Add insert logic here
            return View();
        }
        
        //
        // GET: /Administrator/Faculty/Edit/5
 
        public ActionResult Edit(int id)
        {
            Faculty faculty = _dataContext.Faculties.Single(f => f.FacultyID == id);
            return View(faculty);
        }

        //
        // POST: /Administrator/Faculty/Edit/5

        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(int id, Faculty newFaculty)
        {
            if (ModelState.IsValid)
            {
                try
                {

                    // TODO: Add update logic here
                    Faculty faculty = _dataContext.Faculties.Single(f => f.FacultyID == id);
                    faculty.FacultyName = newFaculty.FacultyName;
                    faculty.FacultyDesciption = newFaculty.FacultyDesciption;
                    faculty.Status = newFaculty.Status;

                    _dataContext.SubmitChanges();
                    return RedirectToAction("Index");


                }
                catch (Exception exception)
                {
                    ViewData["ErrorInfo"] = exception.StackTrace;
                    return View("Error");
                }
            }
            return View();
        }

        //
        // GET: /Administrator/Faculty/Delete/5
 
        public ActionResult Delete(int id)
        {
            Faculty faculty = _dataContext.Faculties.Select(f => f).Where(f=>f.FacultyID==id).Single();
            return View(faculty);
        }

        //
        // POST: /Administrator/Faculty/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                Faculty faculty = _dataContext.Faculties.Single(d => (d.FacultyID == id));
                _dataContext.Faculties.DeleteOnSubmit(faculty);

                _dataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
